﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace ScreenLockerWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ContextMenuControl contextMenuControl = new ContextMenuControl();

        public MainWindow()
        {
            InitializeComponent();
            System.Windows.Forms.Application.EnableVisualStyles();
        }
    }

    public partial class ContextMenuControl : System.Windows.Controls.ContextMenu
    {
        Locker locker = new Locker();
        System.Windows.Forms.NotifyIcon notifyIcon1 = new System.Windows.Forms.NotifyIcon();

        TextBox txtInterval  = new TextBox();
        TextBox txtPause     = new TextBox();
        CheckBox cbAutorun  = new CheckBox();
        Button btnSave      = new Button();
        Button btnExit      = new Button();

        public ContextMenuControl()
        {
            this.Items.Add("Screen Locker");
            this.Items.Add(txtInterval);
            this.Items.Add(txtPause);
            this.Items.Add(cbAutorun);
            this.Items.Add(btnSave);
            this.Items.Add(btnExit);

            this.txtInterval.Text = Properties.Settings.Default.Interval.ToString();
            this.txtPause.Text    = Properties.Settings.Default.PauseTime.ToString();
            cbAutorun.IsChecked   = Properties.Settings.Default.Autorun;
            cbAutorun.Content     = "Autorun";
            btnSave.Content       = "Save";
            btnExit.Content       = "Exit";
            txtInterval.ToolTip   = "Interval";
            txtPause.ToolTip      = "Pause";

            btnSave.Click     += new RoutedEventHandler(btnSave_Click);
            btnExit.Click     += new RoutedEventHandler(btnExit_Click);

            notifyIcon1.Visible = true;
            notifyIcon1.Icon    = Properties.Resources.pause;
            notifyIcon1.Click  += new EventHandler(notifyIcon1_Click);

            locker.Start();
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.IsOpen = true;
        }

        void btnExit_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            locker.Stop();
            System.Windows.Application.Current.Shutdown();
        }

        void btnSave_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                uint interval = Convert.ToUInt32(txtInterval.Text);
                uint pause = Convert.ToUInt32(txtPause.Text);
                bool autorun = cbAutorun.IsChecked.Value;
                locker.SaveSettings(autorun, interval, pause);
            }
            catch 
            {
                MessageBox.Show("Error!");
            }
        }
    }
}
